// PrivateRoute.js
import React, { useEffect } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import { useSelector } from 'react-redux';  // 用于获取 Redux 中的 token
// import { user } from '../store/modules/user'; // 假设你的 Redux store 类型在这里定义

const PrivateRoute = ({ element }) => {
  const navigate = useNavigate();
  const location = useLocation();
//   const token = useSelector((state:any) => state.user.token); // 从 Redux 中获取 token
const token =localStorage.getItem('react_token'); 
  console.log('react_token', token);
  
  useEffect(() => {
    // 如果没有 token，跳转到登录页
    if (!token) {
      navigate('/login', { state: { from: location } });
    }
  }, [token, navigate, location]);

  // 如果 token 存在，则渲染目标组件
  return token ? element : null;
};

export default PrivateRoute;
